﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
using RioLinx.SharePoint.Publishing.Library;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;

namespace RioLinx.SharePoint.Responsive.Tester
{
    class Program
    {
        static void Main(string[] args)
        {
            string strSiteUrl = ConfigurationManager.AppSettings["SiteUrl"];

            string strPageLibraryName = ConfigurationManager.AppSettings["PageLibraryName"];
            string strPageName = ConfigurationManager.AppSettings["PageName"];

            string strTemplateLibraryName = ConfigurationManager.AppSettings["LayoutLibraryName"];

            string strPageDisplayTitle = ConfigurationManager.AppSettings["PageDisplayTitle"];

            bool bUsePublishing = Boolean.Parse(ConfigurationManager.AppSettings["UseFoundationPublishing"]);

            bool bProcessAll = Boolean.Parse(ConfigurationManager.AppSettings["ProcessAll"]);

            if (bProcessAll)
            {
                FileManager.RefreshAllLayout(strSiteUrl, strPageLibraryName, strTemplateLibraryName, bUsePublishing);
            }
            else
            {
                FileManager.RefreshPageFromLayout(strSiteUrl, strPageLibraryName, strPageName, strTemplateLibraryName, strPageDisplayTitle, bUsePublishing);
            }

           
        }

        static void AddTestWebParts(SPWeb web, SPFile page)
        {
            List<string> lstZoneIDs = new List<string>();
            string pageSource = System.Text.Encoding.UTF8.GetString(page.OpenBinary());

            string HTML_ZONE_TAG_PATTERN = "(?i)<WebPartPages:WebPartZone([^>]+)>";
            string HTML_ZONE_ID_TAG_PATTERN = "\\s*(?i)ID\\s*=\\s*(\"([^\"]*\")|'[^']*'|([^'\">\\s]+))";

            string LOREM_IPSUM = @"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.
Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus.
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci.
Aenean nec lorem. In porttitor. Donec laoreet nonummy augue.
Suspendisse dui purus, scelerisque at, vulputate vitae, pretium mattis, nunc. Mauris eget neque at sem venenatis eleifend. Ut nonummy.";

            MatchCollection m1 = Regex.Matches(pageSource, HTML_ZONE_TAG_PATTERN, RegexOptions.Singleline);

            foreach (Match m in m1)
            {
                MatchCollection m2 = Regex.Matches(m.Groups[0].Value, HTML_ZONE_ID_TAG_PATTERN, RegexOptions.Singleline);
                if (m2 != null)
                {
                    lstZoneIDs.Add(m2[0].Groups[1].Value.Replace("\"", ""));
                }

            }

            foreach (string strZoneID in lstZoneIDs)
            {
                using (SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager(page.ServerRelativeUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
                //using (SPLimitedWebPartManager mgr = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
                {
                    ContentEditorWebPart myCEWP = new ContentEditorWebPart();
                    myCEWP.ID = "myCEWP";
                    myCEWP.Title = strZoneID + " Demo Content Editor";
                    myCEWP.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.TitleOnly;
                    XmlElement xmlContent = new XmlDocument().CreateElement("content");
                    xmlContent.InnerText = LOREM_IPSUM;
                    myCEWP.Content = xmlContent;
                    //Adding Webpart to the page
                    mgr.AddWebPart(myCEWP, strZoneID, 1);
                    mgr.SaveChanges(myCEWP);
                }
            }


        }

    }
}
